package com.study.system.controller;


import com.study.common.result.Result;
import com.study.common.utils.JwtHelper;
import com.study.model.book.Book;
import com.study.model.vo.BookQueryVo;
import com.study.system.annotation.Log;
import com.study.system.enums.BusinessType;
import com.study.system.service.BookService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author biyu0723
 * @since 2022-10-30
 */
@Api(tags = "图书管理接口")
@RestController
@RequestMapping("/admin/book/book")
public class BookController {
    @Autowired
    private BookService bookService;


    @ApiOperation("图书列表")
    @GetMapping("/{page}/{limit}")
    public Result list(@PathVariable Long page,
                       @PathVariable Long limit,
                       BookQueryVo bookQueryVo) {
        //创建page对象
        Page<Book> pageParam = new Page<>(page, limit);
        //调用service方法
        IPage<Book> pageModel = bookService.selectPage(pageParam, bookQueryVo);
        return Result.ok(pageModel);
    }

    @ApiOperation("根据id查询")
    @GetMapping("getBook/{id}")
    public Result getUser(@PathVariable String id) {
        Book book = bookService.getById(id);
        return Result.ok(book);
    }

    @Log(title = "图书管理", businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('bnt.book.add')")
    @ApiOperation("添加图书")
    @PostMapping("save")
    public Result save(@RequestBody Book book, HttpServletRequest request) {
        //获取请求头token字符串
        String token = request.getHeader("token");
        //从token字符串获取用户名称（id）
        String username = JwtHelper.getUsername(token);
        book.setCreater(username);
        book.setCreateTime(new Date());
        book.setStatus(0);
        book.setIsDeleted(0);
        boolean is_Success = bookService.save(book);
        if (is_Success) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }
    @Log(title = "图书管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('bnt.book.update')")
    @ApiOperation("修改图书")
    @PostMapping("update")
    public Result update(@RequestBody Book book, HttpServletRequest request) {
        //获取请求头token字符串
        String token = request.getHeader("token");

        //从token字符串获取用户名称（id）
        String username = JwtHelper.getUsername(token);
        book.setUpdater(username);
        book.setUpdateTime(new Date());
        boolean is_Success = bookService.updateById(book);
        if (is_Success) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }
    @Log(title = "图书管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('bnt.book.update')")
    @ApiOperation("更改图书状态")
    @GetMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable String id,
                               @PathVariable Integer status) {
        bookService.updateStatus(id, status);
        return Result.ok();
    }

    @Log(title = "图书管理", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.book.remove')")
    @ApiOperation("删除图书")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable String id) {
        boolean is_Success = bookService.removeById(id);
        if (is_Success) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }
}

